package com.urbanflow.vinci.controler;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.json.simple.parser.ParseException;

import com.urbanflow.vinci.bot.Bot;
import com.urbanflow.vinci.controler.DialogManager.GameType;
import com.urbanflow.vinci.initializator.Initializator;
import com.urbanflow.vinci.main.Arret;

public class BotControler {
	private static BotControler singleton = new BotControler();
	private Bot monBot;
	
	public static BotControler getInstance(){
		return singleton;
	}

	public void demarrerNouvellePartie(String botTokken, String botSecret) throws Exception {
		monBot = new Bot(botTokken, botSecret);
		//Initializator init = Initializator.getInstance();
		//init.initialize();
		List<Arret> lesArrets = new ArrayList<Arret>(Initializator.getInstance().getListeArrets()); 
		ParcoursManager.getInstance().creerReseau(lesArrets);
		DialogManager dManager = new DialogManager(botTokken, botSecret);
		Boolean joinPartie = dManager.JoinGame(GameType.training);
		if(joinPartie) {
			while(!dManager.GameStarted) {
				if(dManager.CheckGameStart()) {
					Thread.sleep(dManager.TimeRemainingBeforeGameStart);
				} else {
					System.out.println(dManager.Erreur);
					throw new Exception();
				}
			}
			if(dManager.GetIncidents()) {
				Integer idDepart = new Integer(dManager.IdArretDepart);
				Integer idFin = new Integer(dManager.IdArretDestination);
				Arret debut = Initializator.getInstance().getArret(idDepart);
				Arret fin = Initializator.getInstance().getArret(idFin);
				ParcoursManager.getInstance().creerLesTrajets(debut, fin, dManager.DateDepart);
				ParcoursManager.getInstance().reduireLeChemin();
				monBot.setLeCheminPrevu(ParcoursManager.getInstance().getLeCheminPrevu());
				monBot.setdManager(dManager);
				monBot.move();
			} 
			else {
				System.out.println(dManager.Erreur);
				throw new Exception();
			}
		}
		else {
			System.out.println(dManager.Erreur);
			throw new Exception();
		}
	}
}
